package com.my.study.tx;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionTemplate;

/**
 * @author Carlos
 * @version 1.0.0
 * @date 2022/11/8 22:15
 */
@Service
public class SysDeptService {
    
    @Autowired
    private SysDeptDao sysDeptDao;
    
    @Autowired
    private TransactionTemplate transactionTemplate;
    
    
    @Transactional(rollbackFor = Exception.class)
    public void insert() {
        sysDeptDao.insert();
        System.out.println("插入成功....");
        update();

        // 编程式事务
        transactionTemplate.execute(status -> {
            try {
                sysDeptDao.update1();
            } catch (Exception e) {
                status.setRollbackOnly();
            }
            return true;
        });
        
        int i = 1 / 0;
    }
    
    
    private void update() {
        sysDeptDao.update(); 
    }
}
